package org.dfl.plugin.dialogs;

import org.dfl.core.model.transitions.TavernaTransition;
import org.dfl.messages.Messages;
import org.dfl.plugin.editor.commands.CancelCommandRuntimeException;
import org.dfl.plugin.editor.commands.TransitionEditProcesorCommand;
import org.dfl.plugin.editor.model.TransitionModelElement;
import org.eclipse.gef.commands.CommandStack;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.embl.ebi.escience.scuflworkers.ProcessorEditor;

/**
 * dialog to edit transition properties for taverna transition
 * @author Piotr Wlodarczyk
 *
 */
public class TavernaTransitionPropertiesDialog extends
		TransitionPropertiesDialog {

	public TavernaTransitionPropertiesDialog(
			TransitionModelElement transitionModelElement) {
		super(transitionModelElement);
	}

	@Override
	protected void addSpecificTransitionElements(Shell parent) {
		final TavernaTransition t = (TavernaTransition) getTransition();
		final ProcessorEditor pe = t.getProcessorEditor();
		if (pe != null) {
			Composite buttons = new Composite(parent, SWT.NONE);
			GridLayout buttonsLayout = new GridLayout();
			buttonsLayout.makeColumnsEqualWidth = true;
			GridData buttonsLData = new GridData();
			buttonsLData.heightHint = 40;
			buttonsLData.grabExcessHorizontalSpace = true;
			buttonsLData.horizontalAlignment = GridData.FILL;
			buttons.setLayoutData(buttonsLData);
			buttons.setLayout(buttonsLayout);
			{
				Composite buttonsCentered = new Composite(buttons, SWT.NONE);
				RowLayout buttonsCenteredLayout = new RowLayout(
						org.eclipse.swt.SWT.HORIZONTAL);
				GridData buttonsCenteredLData = new GridData();
				// buttonsCenteredLData.widthHint = 198;
				buttonsCenteredLData.horizontalAlignment = GridData.CENTER;
				buttonsCenteredLData.grabExcessVerticalSpace = true;
				buttonsCenteredLData.grabExcessHorizontalSpace = true;
				buttonsCenteredLData.verticalAlignment = GridData.FILL;
				buttonsCentered.setLayoutData(buttonsCenteredLData);
				buttonsCentered.setLayout(buttonsCenteredLayout);
				{
					Button okButton = new Button(buttonsCentered, SWT.PUSH
							| SWT.CENTER);
					RowData okButtonLData = new RowData();
					// okButtonLData.width = 250;
					okButtonLData.height = 23;
					okButton.setLayoutData(okButtonLData);
					okButton.setText(Messages.TavernaTransitionPropertiesDialog_title);

					okButton.addListener(SWT.Selection, new Listener() {
						public void handleEvent(Event event) {
							try{
								CommandStack stack = (CommandStack) getTransitionModelElement().getDiagram().getEditor().getAdapter(CommandStack.class);
								stack.execute(new TransitionEditProcesorCommand(getTransitionModelElement()));
								refreshIODescription();
							}catch(CancelCommandRuntimeException e){}
						}
					});
				}
			}
		}
	}

}
